module ApplicationHelper

  def body_class
    controller_name == 'home' && action_name == 'index' ? '' : 'bg-gray'
  end

  def need_bg?
    (controller_path == 'home' && action_name == 'index') ||
    (controller_path == 'users' && action_name == 'new') ||
    (controller_path == 'sessions' && action_name == 'new') ||
    (controller_path == 'abouts' && action_name == 'service_outlets') ||
    (controller_path == 'designers' && action_name == 'index') ||
    (controller_path == 'promotions' && action_name == 'index') ||
    (controller_path == 'wiki' && action_name == 'index')
  end

  def normal_head?
    (controller_name == 'home' && action_name == 'index')
    range = {
              home: ['index'],
              designers: ['index']
            }
    range[controller_name.to_sym].present? && range[controller_name.to_sym].include?(action_name)
  end

  def need_margin?
    (controller_path == 'home' && action_name == 'index') ||
    (controller_path == 'designers' && action_name == 'index') ||
    (controller_path == 'abouts' && action_name == 'service_outlets') ||
    (controller_path == 'promotions' && action_name == 'index')
  end

  def webpack_manifest_script
    return '' unless Rails.configuration.webpack[:use_manifest]
    javascript_tag "window.webpackManifest = #{Rails.configuration.webpack[:common_manifest]}"
  end

  def webpack_bundle_tag(bundle)
    if Rails.env.production?
      src =
        if Rails.configuration.webpack[:use_manifest]
          manifest = Rails.configuration.webpack[:asset_manifest]
          filename = manifest[bundle]

          "#{compute_asset_host}/assets/#{filename}"
        else
          "#{compute_asset_host}/assets/#{bundle}.bundle"
        end

      javascript_include_tag(src) if manifest[bundle].present?
    else
      javascript_include_tag("#{bundle}.entry")
    end
  end

  def get_particular_js
    if (controller_name != controller_path) && (controller_path =~ /\A(\w+)\/\w+$/)
      controller_path.match(/\A(\w+)\/\w+$/)[1]
    else
      js_layout = Setting[:js_layout][controller_name]
      js_layout.nil? ? controller_name : js_layout
    end
  end

  def flash_messages(opts = {})
    flash.each do |msg_type, message|
      concat(content_tag(:div, message, class: "form-messages #{bootstrap_class_for(msg_type)}") do
        concat content_tag(:i, nil, class: "flash-sprite flash-#{msg_type} sprites-icon wiki-common-pointFrmBig-img")
        concat sanitize(message)
      end)
    end
    nil
  end

  def show_add_to_cart product, options = {}
    html_class = "btn btn-danger add-to-cart-btn"
    html_class += " #{options[:html_class]}" unless options[:html_class].blank?

    link_to "<i class='fa fa-spinner'></i> 加入购物车".html_safe, carts_path, class: html_class, 'data-product-id': product.id
  end

  def format_currency currency, digits=2
    ActiveSupport::NumberHelper.number_to_rounded(currency, precision: digits, strip_insignificant_zeros: true)
  end

  def product_small_img(product)
    main_img = product.main_product_image
    if main_img
      image_tag(main_img.image_url(:thumb), alt: product.title)
    else
      image_tag("default_product.png")
    end
  end

  def product_small_img_1(product, class_name)
    main_img = product.main_product_image
    if main_img
      image_tag(main_img.image_url(:thumb), alt: product.title, class: class_name)
    else
      image_tag("default_product.png", class: class_name)
    end
  end

  def product_default_img product, clazz=""
    main_img = product.main_product_image
    if main_img
      image_tag(main_img.image_url, alt: product.title, class: clazz)
    else
      image_tag("default_product.png", class: clazz)
    end
  end

  def wiki_small_img wiki, opts={}
    main_img = wiki.first_img
    if main_img
      image_tag(main_img, alt: wiki.title, class: opts[:class])
    else
      image_tag("default_product.png", class: opts[:class])
    end
  end

  def wiki_small_img_url wiki
    main_img = wiki.first_img
    if main_img
      image_path(wiki.first_img)
    else
      image_path 'default_product.png'
    end
  end

  def common_paginate pagination
    paginate pagination, :left => 2, :window => 2
  end

  def home_mobile_imgs
    [
      {image_url: image_path('mobile/mobile-haibao1.png')},
      {image_url: image_path('mobile/banner@2x.png') },
      {image_url: image_path('mobile/post@2x.png') }
    ]
  end

  def render_page_title
    site_name = Setting.app_name
    title = (@page_title ? "#{@page_title} - #{site_name}" : Setting.seo.main_title) rescue site_name
    content_tag("title", title, nil, false)
  end

  def render_meta_description
    if @meta_description.present?
      site_name = Setting.app_name
      desc = (@meta_description ? "#{@meta_description}" :  '')
      tag(:meta, :name => 'description', :content => desc)
    end
  end

  def render_meta_keywords
    if @meta_keywords.present?
      site_name = Setting.app_name
      keywords = (@meta_keywords ? "#{@meta_keywords}" :  '')
      tag(:meta, :name => 'keywords', :content => keywords)
    end
  end

  def combine_query_params opts
    request.query_parameters.merge(opts).merge(page: 1)
  end

  private
  def bootstrap_class_for flash_type
    { success: "success", error: "error", alert: "warning", notice: "info" }[flash_type.to_sym] || flash_type.to_s
  end
end
